﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Exp {
	public partial class _Default : System.Web.UI.Page {
		protected void Page_Load(object sender, EventArgs e) {
			if (!Page.IsPostBack) {
				RP_Example.DataSource = GetData();
				RP_Example.DataBind();
			}

		}

		public static DataSet GetData() {
			string SQL = @"
						SELECT 
							ROWID,
							LINE_NO,
							PRODUCT_DATE,
							START_TIME,
							SCHDL_NO,
							SEQ_NO,
							ORDER_ITEM_NO
						FROM EXAMPLE
					";
			try {
				using (
					DataSet ds = SqlHelper.ExecuteDataset(
						System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,
						CommandType.Text,
						SQL
					)
				) {
					return ds;
				}
			}
			catch (System.Exception e) {
				throw new Exception("查询失败", e);
			}
		}

		protected void Submit_Click(object sender, EventArgs e) {
			string rowid = ROWID.Text;
			string line_no = LINE_NO.Text;
			string product_date = PRODUCT_DATE.Text;
			string start_time = START_TIME.Text;
			string schdl_no = SCHDL_NO.Text;
			string order_item_no = ORDER_ITEM_NO.Text;

			string SQL = @"
						SELECT 
							ROWID,
							LINE_NO,
							PRODUCT_DATE,
							START_TIME,
							SCHDL_NO,
							SEQ_NO,
							ORDER_ITEM_NO
						FROM EXAMPLE 
						WHERE ";

			string Where = " 1=1 ";
			
			if (!string.IsNullOrEmpty(line_no)) {
				Where += " AND LINE_NO LIKE '%" + line_no + "%' ";
			}
			if (!string.IsNullOrEmpty(product_date)) {

				DateTime dt;
				if (DateTime.TryParse(product_date, out dt)) {
					Where += " AND PRODUCT_DATE = '" + dt.ToString() + "' ";
				}
			}
			if (!string.IsNullOrEmpty(start_time)) {
				Where += " AND START_TIME LIKE '%" + start_time + "%'  ";
			}
			if (!string.IsNullOrEmpty(schdl_no)) {
				Where += " AND SCHDL_NO = " + schdl_no + " ";
			}
			if (!string.IsNullOrEmpty(order_item_no)) {
				Where += " AND ORDER_ITEM_NO LIKE '%" + order_item_no + "%' ";
			}

			try {
				using (
					DataSet ds = SqlHelper.ExecuteDataset(
						System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString,
						CommandType.Text,
						SQL + Where
					)
				) {
					RP_Example.DataSource = ds;
					RP_Example.DataBind();
				}
			}
			catch (System.Exception ex) {
				throw new Exception("查询失败", ex);
			}
		}

		protected void Edit_Click(object sender, EventArgs e) {
			
		}
	}
}
